package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.*;
import com.acme.bankapp.domain.bank.exceptions.*;
import sun.plugin.converter.resources.Converter_ja;

import java.io.File;
import java.util.Random;

/**
 * Created by anton on 8/18/14.
 */
class BankApplication {
    private static void createAccounts(Bank bank) {
        try {
            BankService.addClient(bank,
                    ClientBuilder.newClient().withName("Tatiana").female().withAccount(new SavingsAccount(200.0)).build());
            BankService.addClient(bank,
                    ClientBuilder.newClient().withName("Stefan").male().withAccount(new SavingsAccount(202.0)).build());
            BankService.addClient(bank,
                    ClientBuilder.newClient().withName("Irina").female().withAccount(new SavingsAccount(231.0)).build());
            BankService.addClient(bank,
                    ClientBuilder.newClient().withName("John").male().withAccount(new SavingsAccount(43212.0)).build());
            BankService.addClient(bank,
                    ClientBuilder.newClient().withName("Ernest").male().withAccount(new SavingsAccount(5433.0)).build());
            BankService.addClient(bank,
                    ClientBuilder.newClient().withName("Linda").female().withAccount(new CheckingAccount(400.0, 100.0)).build());
            BankService.addClient(bank,
                    ClientBuilder.newClient().withName("Valentino").male().withAccount(new CheckingAccount(50.0, 100.0)).build());
            BankService.addClient(bank,
                    ClientBuilder.newClient().withName("Roberta").female().withAccount(new CheckingAccount(300.0, 100.0)).build());
            BankService.addClient(bank,
                    ClientBuilder.newClient().withName("Benjamin").male().withAccount(new CheckingAccount(1000.0, 1000.0)).build());
            BankService.addClient(bank,
                    ClientBuilder.newClient().withName("Serena").female().withAccount(new CheckingAccount(200.0, 1000.0)).build());
        } catch (ClientExistsException e) {
            System.out.println("Client with name " + e.getClientName() + " already exists");
        }
    }

    private static void modifyBank(Bank bank) {
        try {
            bank.getClientByName("Linda").getSingleAccount().withdraw(500.0);
            bank.getClientByName("Valentino").getSingleAccount().withdraw(100.0);
            bank.getClientByName("Roberta").getSingleAccount().withdraw(350.0);
            bank.getClientByName("Benjamin").getSingleAccount().withdraw(1500.0);
            assert BankReport.getBankCredit(bank) == 700.0 : "Error in calculating bank credit";
        } catch (NotEnoughFundsException e) {
            e.printStackTrace();
        }
    }

    private static void printBalance(Bank bank) {
        for (Client c : bank.getClients()) {
            System.out.println("Client " + c + " has balance " + c.getClientBalance());
        }
    }

    public static void main(String[] args) {
        switch (args.length) {
            case 0:
                flow1();
                break;
            case 2:
                flow2(args);
                break;
            case 3:
                flow3(args);
                break;
            default:
                System.out.println("Cannot handle the program. Exiting...");
                break;
        }



    }


    private static void flow1() {
        Bank bank = new Bank();
        BankApplication.createAccounts(bank);
//        bank.closeEmailService();
        BankApplication.modifyBank(bank);
        BankApplication.printBalance(bank);
        System.out.println(BankReport.getAccountsSortedBySum(bank));
//        BankService.printMaximumAmountToWithdraw(bank);
    }

    private static void flow2(String[] args) {

    }

    private static void flow3(String... args) {
        try {
            File file = new File(args[1]);
            Bank bank = new Bank();
            if (args[0].equals("-loadfeed") && file.canRead()) {
                BankDataLoaderService.loadBank(bank, file);
                BankService.saveBank(bank);
            }
            if (args[2].equals("-server")) {
                BankServer.startServer(bank);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}